package com.buba.springcloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.springcloud.mapper.FeedbackMapper;
import com.buba.springcloud.mapper.MaterialMapper;
import com.buba.springcloud.mapper.StoreAdviceMapper;
import com.buba.springcloud.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
public class FeedbackController {

    //注入服务发现的注解
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private StoreAdviceMapper storeAdviceMapper;


    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "feedback/consul")
    public String FeedbackConsul() {
        return "SpringCloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString();
    }

//    @CrossOrigin(methods = RequestMethod.GET)
//    @GetMapping("/Feedback/get/{id}")
//    public CommonResult queryById(@PathVariable("id") Long id){
//        SuppliesAround suppliesAround = materialMapper.selectById(id);
//        log.info("***************查询成功*********"+suppliesAround);
//        if(suppliesAround!=null){
//            return new CommonResult(200,"查询成功",suppliesAround);
//        }else{
//            return new CommonResult(444,"查询失败",null);
//        }
//    }

    @CrossOrigin(methods = RequestMethod.GET)
    @GetMapping("/feedback/getList")
    public CommonResult getList(){
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        List<Feedback> list = feedbackMapper.selectList(queryWrapper);
//        log.info("***************查询成功*********"+list);
        if(list!=null){
            return new CommonResult(200,"查询成功",list);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

    /**
     * 多媒体模块留言
     * @param feedback
     * @return
     */
    @CrossOrigin(methods = RequestMethod.POST)
    @PostMapping("/feedback/save")
    public CommonResult saveMaterialApplication(@RequestBody(required=false) Feedback feedback){
        log.info("***************开始保存*********");

        int result = this.feedbackMapper.insert(feedback);
        log.info("***************保存申请成功*********"+result);
        if(result!=0){
            return new CommonResult(200,"保存成功",feedback);
        }else{
            return new CommonResult(444,"保存失败",null);
        }
    }

    //获取服务信息
    @GetMapping("/feedback/discovery")
    public  Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String s : services){
            log.info("********注册到eureka中的服务中有:"+services);
        }
        List <ServiceInstance> instances = discoveryClient.getInstances("epidemic-material");
        for (ServiceInstance s: instances) {
            log.info("当前服务的实例有"+s.getServiceId()+"\t"+s.getHost()+"\t"+s.getPort()+"\t"+s.getUri());
        }
        return this.discoveryClient;
    }

}
